package com.stylefeng.guns.modular.business.controller;

import com.stylefeng.guns.command.CommandExecute;
import com.stylefeng.guns.command.impl.SendCmd;
import com.stylefeng.guns.common.constant.state.BarrageStatus;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.BarrageMapper;
import com.stylefeng.guns.common.persistence.model.Barrage;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.warpper.BarrageWarpper;
import com.stylefeng.guns.modular.system.warpper.DeptWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 弹幕管理控制器
 *
 * @author fengshuonan
 * @Date 2018-08-18 15:57:09
 */
@Controller
@RequestMapping("/barrage")
public class BarrageController extends BaseController {


    private ConcurrentHashMap threadMap = new ConcurrentHashMap();

    private String PREFIX = "/business/barrage/";

    @Autowired
    private BarrageMapper barrageMapper;

    @Autowired
    private CommandExecute commandExecute;

    /**
     * 跳转到弹幕管理首页
     */
    @RequestMapping("")
    public String index() {

        return PREFIX + "barrage.html";
    }

    /**
     * 跳转到添加弹幕管理
     */
    @RequestMapping("/barrage_add")
    public String barrageAdd() {
        return PREFIX + "barrage_add.html";
    }

    /**
     * 跳转到修改弹幕管理
     */
    @RequestMapping("/barrage_update/{barrageId}")
    public String barrageUpdate(@PathVariable Integer barrageId, Model model) {
        Barrage barrage = barrageMapper.selectByPrimaryKey(barrageId);
        model.addAttribute("barrage", barrage);
//        model.addAttribute("pName", ConstantFactory.me().getDeptName(dept.getPid()));
//        LogObjectHolder.me().set(dept);
        return PREFIX + "barrage_edit.html";
    }

    /**
     * 获取弹幕管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {

        List<Map<String, Object>> list = barrageMapper.list(condition);
        return super.warpObject(new BarrageWarpper(list));
    }

    /**
     * 新增弹幕管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Barrage barrage) {
        if (ToolUtil.isOneEmpty(barrage, barrage.getUsername())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        barrage.setStatus(BarrageStatus.INIT.getCode());
        return barrageMapper.insert(barrage);
    }

    /**
     * 删除弹幕管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer barrageId) {

        barrageMapper.deleteByPrimaryKey(barrageId);
        return SUCCESS_TIP;
    }


    /**
     * 修改弹幕管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Barrage barrage) {
        if (ToolUtil.isOneEmpty(barrage, barrage.getId())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        barrageMapper.updateByPrimaryKeySelective(barrage);
        return super.SUCCESS_TIP;
    }

    /**
     * 弹幕管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }


    /**
     * 删除弹幕管理
     */
    @RequestMapping(value = "/start")
    @ResponseBody
    public Object start(@RequestParam Integer barrageId) {
        Barrage barrage = barrageMapper.selectByPrimaryKey(barrageId);
        SendCmd sendCmd = new SendCmd(barrage);
        commandExecute.execute(sendCmd, "barrage_" + barrageId);
        return SUCCESS_TIP;
    }
}
